//
//  MentionedMessageController.m
//  collection
//
//  Created by 靖 张 on 12/14/11.
//  Copyright (c) 2011 Tsinghua University. All rights reserved.
//

#import "MentionedMessageController.h"
#import "CollectionDataManager.h"
#import "MentionedMessage.h"
#import "Utility.h"
#import "UserProfileController.h"
#import "AppDelegate.h"

#define MENTIONED_MSG_DESC  @"已经关注了您"

@interface MentionedMessageController()

@property (nonatomic, strong)   IBOutlet    UITableView*    tableView;
@property (nonatomic, strong)   NSArray*    mentionedMsgList;

@end

@implementation MentionedMessageController

@synthesize tableView   = _tableView;
@synthesize mentionedMsgList = _mentionedMsgList;

@synthesize homepageRootController = _homepageRootController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc {
    
    self.homepageRootController = nil;
    self.tableView = nil;
    self.mentionedMsgList = nil;
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UIView* emptyView = [[UIView alloc] init];
    emptyView.backgroundColor = [UIColor clearColor];
    self.tableView.tableFooterView = emptyView;
    [emptyView release];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.homepageRootController = nil;
    self.tableView = nil;
    self.mentionedMsgList = nil;
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self refresh];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Implementation of UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [self.mentionedMsgList count];
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString* mentionedMsgCellID = @"MentionedMessageCell";
    
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:mentionedMsgCellID];
    if (cell == nil) {
        NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"MentionedMessageCell" owner:self options:nil];
        cell = [views objectAtIndex:0];
        UIView* bgView = [[UIView alloc] initWithFrame:cell.frame];
        bgView.backgroundColor = [UIColor colorWithRed:229.0 / 255.0 green:229.0 / 255.0 blue:229.0 / 255.0 alpha:1];
        cell.selectedBackgroundView = bgView;
        [bgView release];
    }
    
    MentionedMessage* mentionedMsg = [self.mentionedMsgList objectAtIndex:indexPath.row];
    
    CGRect frame;
    // Avatar
    UIImageView* avatarImage = (UIImageView*)[cell viewWithTag:1];
    avatarImage.image = mentionedMsg.avatar;
    
    // Mentioned UserID
    UILabel* userNameLabel = (UILabel*)[cell viewWithTag:2];
    userNameLabel.text = mentionedMsg.userName;
    [userNameLabel sizeToFit];
    
    // Text
    UILabel* msgDescriptionLabel = (UILabel*)[cell viewWithTag:3];
    msgDescriptionLabel.text = MENTIONED_MSG_DESC;
    frame = msgDescriptionLabel.frame;
    frame.origin.x = userNameLabel.frame.origin.x + userNameLabel.frame.size.width + 2;
    msgDescriptionLabel.frame = frame;
    
    // Time stamp
    UILabel* timestampLabel = (UILabel*)[cell viewWithTag:4];
    timestampLabel.text = [Utility transTimeToCurrentFormat:mentionedMsg.timestamp];
    
    return cell;
}

#pragma mark - Implementation of UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell* cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UserProfileController* userProfileController = [[UserProfileController alloc] initWithNibName:@"UserProfileController" bundle:nil];
    MentionedMessage* mentionedMsg = [self.mentionedMsgList objectAtIndex:indexPath.row];
    userProfileController.userID = mentionedMsg.userID;
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    [appDelegate.rootNavigationController pushViewController:userProfileController animated:YES];
    [userProfileController release];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.0f;
}

#pragma mark - Public methods

- (void)refresh {
    
    CollectionDataManager* sharedDataManager = [CollectionDataManager sharedInstance];
    self.mentionedMsgList = sharedDataManager.mentionedMsgList;
    [self.tableView reloadData];
}

@end
